package database;

import java.util.*;
import sharedspace.*;

/**
 * Responsible for data transition from SQL server to the server
 * @author G3
 *
 */
public class TestControl {
	
	private Test test = null;
	private List<Test> list = null;
	private ManualTest manualTest=null;
		
	public TestControl(){	
	}
	
	/**
	 * Generate list of all tests in database
	 * @return ArrayList<Test> list
	 */
	public List<Test> getTestList()
	{
		
		int testSize = JDBC.countStatment("SELECT count(*) FROM test t");
		list = new ArrayList<Test>();
		Object[][] result = new Object[testSize][Test.NUM_OF_COLUMNS];
		JDBC.selectStatmentMatrix("SELECT * FROM test t", result, Test.NUM_OF_COLUMNS, testSize);
		for (int i = 0; i<testSize; i++)
		{
			test = new Test();
			setTest(result, i);
			list.add(test);
		}
		
		return list;
	}
	
	/**
	 * Generate list of tests by subject
	 * @param subjectID
	 * @return ArrayList<Test> list
	 */
	public List<Test> getTestListBySubjectID(String subjectID)
	{
		int testSize = JDBC.countStatment("SELECT count(*) FROM test t WHERE t.subjectID=" + subjectID + ";");
		list = new ArrayList<Test>();
		Object[][] result = new Object[testSize][Test.NUM_OF_COLUMNS];
		JDBC.selectStatmentMatrix("SELECT * FROM test t WHERE t.subjectID=" + subjectID + ";", result, Test.NUM_OF_COLUMNS, testSize);
		for (int i = 0; i<testSize; i++)
		{
			test = new Test();
			setTest(result,i);
			list.add(test);
		}
		
		return list;
	}
	
	/**
	 * Generate list of tests by course and subject
	 * @param courseID
	 * @param subjectID
	 * @return ArrayList<Test> list
	 */
	public List<Test> getTestListByCourseID(String courseID, String subjectID)
	{
		int testSize = JDBC.countStatment("SELECT count(*) FROM test t WHERE t.subjectID="+ subjectID + ";");
		test = new Test();
		list = new ArrayList<Test>();
		ArrayList<Test> tlist=new ArrayList<Test>();
		Object[][] result = new Object[testSize][Test.NUM_OF_COLUMNS];
		JDBC.selectStatmentMatrix("SELECT * FROM test t WHERE t.subjectID="+ subjectID + ";", result, Test.NUM_OF_COLUMNS, testSize);
		for(int i = 0; i<testSize; i++)
		{
			test = new Test();
			setTest(result,i);
			list.add(test);
		}
		
		List<String> stringsList = new ArrayList<String>();
		for (int i = 0; i<list.size(); i++)
			stringsList.add(list.get(i).getTestID());
		Collections.sort(stringsList);
		String str;
		int i;
		for ( i = 0; i<stringsList.size(); i++){
			str = stringsList.get(i).substring(2,4);
			if (str.endsWith(courseID))
				tlist.add(list.get(i));
			} 
		return tlist;
		
	}
	
	/**
	 * Generate list of tests by courseID and subjectID
	 * @param courseID
	 * @param subjectID
	 * @return ArrayList<Test> list
	 */
	public List<Test> getTestListByCourseSubject(String courseID, String subjectID)
	{
		int testSize = JDBC.countStatment("SELECT count(*) FROM test t WHERE t.testID LIKE '"+subjectID + courseID + "__';");
		
		list = new ArrayList<Test>();
		
		Object[][] result = new Object[testSize][Test.NUM_OF_COLUMNS];
		JDBC.selectStatmentMatrix("SELECT * FROM test t WHERE t.testID LIKE '"+subjectID + courseID + "__';"
								, result, Test.NUM_OF_COLUMNS, testSize);
		for (int i = 0; i<testSize; i++)
		{
			test = new Test();
			setTest(result,i);
			list.add(test);
		}

		return list;
	}
	
	/**
	 * Generate list of tests that was built by specific teacher
	 * @param teacherID
	 * @return ArrayList<Test> list
	 */
	public List<Test> getTestListByTeacherID(String teacherID)
	{
		int testSize = JDBC.countStatment("SELECT count(*) FROM test t WHERE t.author="+ teacherID + ";");
		list = new ArrayList<Test>();
		Object[][] result = new Object[testSize][Test.NUM_OF_COLUMNS];
		JDBC.selectStatmentMatrix("SELECT * FROM test t WHERE t.author="+ teacherID + ";", result, Test.NUM_OF_COLUMNS, testSize);
		for(int i = 0; i<testSize;i++)
		{
			test = new Test();
			setTest(result,i);
			list.add(test);
		}
		
			
		return list;
		
	}
	
	/**
	 * Initialize Test object from database result
	 * @param result
	 * @param row
	 */
	public void setTest(Object[][] result, int row)
	{
		test.setTestID(result[row][0].toString());
		test.setTestDuration(Integer.parseInt(result[row][1].toString()));
		test.setCommentForTeacher(result[row][2].toString());
		test.setCommentForStudent(result[row][3].toString());
		test.setAuthor((new TeacherControl()).getTeacherByTeacherID(result[row][4].toString()));
		test.setSubject((new SubjectControl()).getSubjectBySubjectID(result[row][5].toString()));
		test.setCourse((new CourseControl()).getCourseByCourseID(test.getTestID().substring(2, 4)));
	}
	
	/**
	 * Initialize Test object from database result
	 * @param result
	 */
	public void setTest(Object[] result)
	{
		test.setTestID(result[0].toString());
		test.setTestDuration(Integer.parseInt(result[1].toString()));
		test.setCommentForTeacher(result[2].toString());
		test.setCommentForStudent(result[3].toString());
		test.setAuthor((new TeacherControl()).getTeacherByTeacherID(result[4].toString()));
		test.setSubject((new SubjectControl()).getSubjectBySubjectID(result[5].toString()));
		test.setCourse((new CourseControl()).getCourseByCourseID(test.getTestID().substring(2, 4)));
	}

	/**
	 * return test by testID
	 * @param testID
	 * @return Test test
	 */
	public Test getTestByTestID(String testID)
	{
		test = new Test();
		Object[] result = new Object[Test.NUM_OF_COLUMNS];
		JDBC.selectStatment("SELECT * FROM test t WHERE t.testID=" + testID + ";", result, Test.NUM_OF_COLUMNS);
		setTest(result);
		return test;
		
	} 
	
	/**
	 * allocate new ID for test when subject and course ID are given
	 * @param subjectID
	 * @param courseID
	 * @param test
	 * @return String of Test Number
	 */
	public String AllocationTest(String subjectID,String courseID,Test test)
	{
		
		List<Test> list = this.getTestListByCourseSubject(courseID, subjectID);
		if (list.size() == 0)
		{
			test.setTestID(subjectID+courseID+"01");
			JDBC.insertStatment("INSERT into test "+
					"value('"+test.getTestID()+"','"+test.getTestDuration()+"','"+test.getCommentForTeacher()+"','"+test.getCommentForStudent()+
					"','"+test.getAuthor().getUserID()+"','"+subjectID+"');");
			return (subjectID+courseID+"01") ;		
		}
		List<String> stringsList = new ArrayList<String>();
		for (int i = 0; i<list.size(); i++)
			stringsList.add(list.get(i).getTestID());
		Collections.sort(stringsList);
		Integer testID = stringsList.size();
		String str = stringsList.get(testID-1);
		str = str.substring(4);
		testID = Integer.parseInt(str);
		testID++;
		str = testID.toString();
		int length = str.length();
		switch (length)
		{

			case 1: 
				String newst = new String("0");
				str = newst+str;
				break;
			case 2: 
		
		}
	
		str = subjectID+courseID+str;
		test.setTestID(str);
		
		JDBC.insertStatment("INSERT into test "+
							"value('"+test.getTestID()+"','"+test.getTestDuration()+"','"+test.getCommentForTeacher()+"','"+test.getCommentForStudent()+
							"','"+test.getAuthor().getUserID()+"','"+subjectID+"');");
		return str;
	}
	
	public String AllocationManual(ManualTest manualTest)
	{
		
			JDBC.insertStatment("INSERT into manualtest "+
					"values('"+manualTest.getTestID()+"','"+manualTest.getPath()+"');");
			return manualTest.getTestID();		
		}
	
	/**
	 * Gets the 
	 * @param testID
	 * @return path of manual test
	 */
	public ManualTest ManualTestbyTestID(String testID)
	{
		
		ManualTest manualtest;
		Object[] result = new Object[ManualTest.NUM_OF_COLUMNS];
		JDBC.selectStatment("SELECT * FROM manualtest t WHERE t.testID='" + testID + "';", result, ManualTest.NUM_OF_COLUMNS);
		manualtest = setManualTest(result);
		return manualtest;
		
	}
	
	public ManualTest setManualTest(Object[] result)
	{
		ManualTest manualTest = new ManualTest();
		manualTest.setTestID(result[0].toString());
		manualTest.setPath(result[1].toString());
		Test test = getTestByTestID(result[0].toString());
		manualTest.setAuthor(test.getAuthor());
		manualTest.setCommentForStudent(test.getCommentForStudent());
		manualTest.setCommentForTeacher(test.getCommentForTeacher());
		manualTest.setCourse(test.getCourse());
		manualTest.setSubject(test.getSubject());
		manualTest.setTestDuration(test.getTestDuration());
		return manualTest;
	}
	/* public static void main(String[] args) //debug
	 {
		 TestControl tc = new TestControl();
		 Test test = new Test();
		 List<Test> list = new ArrayList<Test>();

		 test = tc.getTestByTestID("1");
		 System.out.println(test.getAuthor().getUserFirstName() + " " + test.getAuthor().getUserLastName());
		 list = tc.getTestList();
		 System.out.println(list.get(1).getAuthor().getUserFirstName() + " " + list.get(1).getAuthor().getUserLastName());
		 list = tc.getTestListBySubjectID("1");
		 System.out.println(list.get(0).getAuthor().getUserFirstName() + " " + list.get(0).getAuthor().getUserLastName());
		 list = tc.getTestListByCourseID("1");

		 
	 }*/
}
